package com.example.cassandra_demo.repository;

import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
import com.example.cassandra_demo.model.HotelByLetter;
import com.example.cassandra_demo.model.HotelByLetterKey;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: Crystal
 * @Date: 2019/1/22 14:12
 **/
@Repository
public class CassandraHotelByLetterRepository implements HotelByLetterRepository {

    private final CassandraTemplate cassandraTemplate;

    public CassandraHotelByLetterRepository(CassandraTemplate cassandraTemplate) {
        this.cassandraTemplate = cassandraTemplate;
    }

    @Override
    public List<HotelByLetter> findByFirstLetter(String letter) {
        Select select = QueryBuilder.select().from("hotels_by_letter");
        select.where(QueryBuilder.eq("first_letter", letter));
        return this.cassandraTemplate.select(select, HotelByLetter.class);
    }

    @Override
    public HotelByLetter save(HotelByLetter hotelByLetter) {
        return this.cassandraTemplate.insert(hotelByLetter);
    }

    @Override
    public void delete(HotelByLetterKey hotelByLetterKey) {
        this.cassandraTemplate.deleteById(hotelByLetterKey, HotelByLetter.class);
    }
}
